#!/usr/bin/env python3
# _*_ coding:utf-8 _*_

import functools
from django.conf import settings
from Arc.utils.mark import ArcHandler


class PermissionHandler(object):
    def add_btn(self, request, *args, **kwargs):
        permission_dict = request.session.get(settings.PERMISSION_SESSION_KEY)
        if self.get_url_name('add') not in permission_dict:
            return None
        return super().add_btn(request, *args, **kwargs)

    def get_display_list(self, request, *args, **kwargs):
        """
        获取页面上应该显示的列，预留的自定义扩展，例如：以后根据用户的不同显示不同的列
        :return:
        """
        value = []
        if self.display_list:
            value.extend(self.display_list)
            p_dict = request.session.get(settings.PERMISSION_SESSION_KEY)
            e_url = self.get_url_name('edit')
            d_url = self.get_url_name('del')
            if e_url in p_dict and d_url in p_dict:
                value.append(type(self).display_edit_del)
            elif e_url in p_dict:
                value.append(type(self).display_edit)
            elif d_url in p_dict:
                value.append(type(self).display_del)
        return value

    def permission(self, func):
        @functools.wraps(func)
        def inner(*args, **kwargs):
            return func

        return inner
